package cc.zy.base.hr.service.impl;


import cc.zy.base.common.entity.QywxDeptTree;
import cc.zy.base.common.utils.TreeUtil;
import cc.zy.base.hr.entity.QywxDept;
import cc.zy.base.hr.mapper.QywxDeptMapper;
import cc.zy.base.hr.service.IQywxDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author LiuShuiWang
 * @date 2022/3/8 , 23:27
 * @Description: 部门
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class QywxDeptServiceImpl extends ServiceImpl<QywxDeptMapper, QywxDept> implements IQywxDeptService {

    @Resource
    private final QywxDeptMapper qywxDeptMapper;

    /**
     查所有 树形列表
     * @Author LiuWang
     * @Date 2022/3/9 20:04
     */
    @Override
    public List<QywxDeptTree<QywxDept>> selectQywxDept(){

        List<QywxDept> List = qywxDeptMapper.findDeptDetail();
        List<QywxDeptTree<QywxDept>> trees = this.converQywxDept(List);
        return TreeUtil.buildQywxDeptTree(trees);

    }

    /**
     将QywxDept 装换成 QywxDeptTree
     * @Author LiuWang
     * @Date 2022/3/9 10:25
     *转换 QywxDept  为DeptTree
     */
    private List<QywxDeptTree<QywxDept>> converQywxDept(List<QywxDept> QywxDept) {

        List<QywxDeptTree<QywxDept>> trees = new ArrayList<>();

        QywxDept.forEach( qywxDept -> {
            QywxDeptTree<QywxDept> tree = new QywxDeptTree<>();
            //部门ID
            tree.setId(String.valueOf(qywxDept.getId()));
            //父ID
            tree.setParentId(String.valueOf(qywxDept.getParentId()));
            //名字
            tree.setName(qywxDept.getDeptName());
            //子级
            tree.setData(qywxDept);
            //
            trees.add(tree);
        });

        return trees;

    }

}
